new Vue({
    data: {
        menu1: 'first',
        menu2:'',
        login: true,
        userId:null,
        userName:null,
        likes:[],
        subscribes:[],
        comments:[],
        chats:[],
        subscribe:{
            "subsriber": {
                "userId":null
            },
            "beSubsribedId": null
        },
        currentChatId:null,
        currentChatRecord:[],
        currentChatUserName:null,
        chat:{
            "chatRecordContent":null,
            "chatId": null,
            "userId": null,
            "chatRecordDate":null
        }
    },
    methods: {
        subscribeSomeone:function (beSubscribedId){
            this.subscribe.subsriber.userId=this.userId;
            this.subscribe.beSubsribedId=beSubscribedId;
            axios.post("subscribe/subscribeToSomeone.do",this.subscribe).then(resp=>{
                alert("关注成功");
                window.location.reload();
            });
        },
        openChat:function (chatRoomId,chatToUserName){
            this.currentChatId=chatRoomId;
            this.currentChatUserName=chatToUserName;
            axios.get("chat/getChatRecord.do?chatId="+chatRoomId).then(resp=>{
               this.currentChatRecord=resp.data;
            });
        },
        sendMessage:function (){
            this.chat.chatId=this.currentChatId;
            this.chat.userId=this.userId;
            const date = new Date();
            const year = date.getFullYear().toString().padStart(4, "0");
            const month = (date.getMonth() + 1).toString().padStart(2, "0");
            const day = date.getDate().toString().padStart(2, "0");
            const hour = date.getHours().toString().padStart(2, "0");
            const minute = date.getMinutes().toString().padStart(2, "0");
            const second = date.getSeconds().toString().padStart(2, "0");
            console.log(`${year}-${month}-${day} ${hour}:${minute}:${second}`); // 2023-02-16 08:25:05
            this.chat.chatRecordDate=`${year}-${month}-${day} ${hour}:${minute}:${second}`;

            var chat={
                    "chatRecordContent":this.chat.chatRecordContent,
                    "chatId": this.chat.chatId,
                    "userId": this.chat.userId,
                    "chatRecordDate":this.chat.chatRecordDate
            }

            this.currentChatRecord.push(chat);
            // console.log("this.chats",this.chats);
            axios.post("chat/addChatRecord.do",chat).then(resp=>{
            });
            this.chat.chatRecordContent='';

            let scrollDiv = document.getElementById("scrollDiv");
            scrollDiv.scrollTop=scrollDiv.scrollHeight-scrollDiv.clientHeight;
        }
    },
    mounted(){

        /**
         * 获得谁点赞了你的文章
         */
        this.userId=document.getElementById("userId").value;
        axios.get("likes/getLiked.do?userId="+this.userId).then(resp=>{
            this.likes=resp.data;
            // console.log(this.likes);
        });

        /**
         * 获得谁关注了你
         */
        axios.get("subscribe/getSubscribed.do?userId="+this.userId).then(resp=>{
           this.subscribes=resp.data;
        });

        /**
         * 获得谁评论了你的文章
         */
        axios.get("comment/getCommentOnMe.do?userId="+this.userId).then(resp=>{
           this.comments=resp.data;
        });

        /**
         * 获得你的姓名
         */
        axios.get("user/getUserById.do?userId="+this.userId).then(resp=>{
            this.userName=resp.data.userName;
        })

        /**
         * 获得聊天室
         */
        axios.get("chat/getMy.do?userId="+this.userId).then(resp=>{
            this.chats=resp.data;
        });
    }
}).$mount("#app");